package com.dg.river.core.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;

import com.dg.river.R;
import com.dg.river.core.MyApplication;
import com.dg.river.core.view.dialog.ShareDialog;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;

/**
 * Created by hy on 2018/11/7
 * Description : 分享工具类
 */
public class ShareUtils {

    public static final int SHARE_TO_WX_FRIEND = SendMessageToWX.Req.WXSceneSession;
    public static final int SHARE_TO_WX_SPACE = SendMessageToWX.Req.WXSceneTimeline;

    public interface onShareWeChatFriendListener {
        void onWeChatFriend();
    }

    public interface onShareWeChatSpaceListener {
        void onWeChatSpace();
    }

    public interface onShareQQListener {
        void onQQ();
    }

    /**
     * 显示分享Dialog
     *
     * @param mContext
     * @param onShareQQListener
     * @param onShareWeChatFriendListener
     * @param onShareWeChatSpaceListener
     */
    public static void showShareDialog(Context mContext,
                                       final onShareQQListener onShareQQListener,
                                       final onShareWeChatFriendListener onShareWeChatFriendListener,
                                       final onShareWeChatSpaceListener onShareWeChatSpaceListener) {
        ShareDialog shareDialog = ShareDialog.show(mContext, true);
        shareDialog.setQQClickListener(new ShareDialog.OnQQClickListener() {
            @Override
            public void onItemClick(View view) {
                onShareQQListener.onQQ();
            }
        });
        shareDialog.setWeChatFriendClickListener(new ShareDialog.OnWeChatFriendClickListener() {
            @Override
            public void onItemClick(View view) {
                onShareWeChatFriendListener.onWeChatFriend();
            }
        });
        shareDialog.setWeChatSpaceClickListener(new ShareDialog.OnWeChatSpaceClickListener() {
            @Override
            public void onItemClick(View view) {
                onShareWeChatSpaceListener.onWeChatSpace();
            }
        });
    }

    /**
     * 分享网页到微信
     *
     * @param context     context对象
     * @param url         分享的网页地址
     * @param title       标题
     * @param description 描述
     * @param drawable    分享头像
     * @param toShare     分享对象：朋友圈或好友会话
     */
    public static void sharePageToWx(Context context, String url, String title, String description, int drawable, int toShare) {
        if (MyApplication.getInstance().wxapi.isWXAppInstalled()) {
            WXWebpageObject webPageObject = new WXWebpageObject();
            webPageObject.webpageUrl = url;

            WXMediaMessage msg = new WXMediaMessage(webPageObject);
            msg.title = title;
            msg.description = description;
            Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), drawable);
            msg.thumbData = Utils.Bitmap2Bytes(thumb);

            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = "";//唯一标识
            req.message = msg;
            req.scene = toShare;
            MyApplication.getInstance().wxapi.sendReq(req);
        } else {
            ToastUtils.getInstance().toast("请先安装微信");
        }
    }

    /**
     * 分享网页到微信
     *
     * @param context     context对象
     * @param url         分享的网页地址
     * @param title       标题
     * @param description 描述
     * @param drawable    分享头像
     * @param toShare     分享对象：朋友圈或好友会话
     */
    public static void sharePageToWx(final Context context, final String url, final String title, final String description, final String drawable, final int toShare) {
        if (MyApplication.getInstance().wxapi.isWXAppInstalled()) {
            WXWebpageObject webPageObject = new WXWebpageObject();
            webPageObject.webpageUrl = url;

            final WXMediaMessage msg = new WXMediaMessage(webPageObject);
            msg.title = title;
            msg.description = description;

            new BitmapUtils().getImage(drawable, new BitmapUtils.HttpCallBackListener() {
                @Override
                public void onFinish(Bitmap bitmap) {
                    if (bitmap != null) {
                        msg.thumbData = Utils.bmpToByteArray(bitmap, true);
                    } else {
                        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
                        msg.thumbData = Utils.Bitmap2Bytes(thumb);
                    }
                    SendMessageToWX.Req req = new SendMessageToWX.Req();
                    req.transaction = "";//唯一标识
                    req.message = msg;
                    req.scene = toShare;
                    MyApplication.getInstance().wxapi.sendReq(req);
                }

                @Override
                public void onError(Exception e) {

                }
            });
        } else {
            ToastUtils.getInstance().toast("请先安装微信");
        }
    }

    /**
     * 分享图片到微信
     *
     * @param context  context对象
     * @param drawable 分享头像
     * @param toShare  分享对象：朋友圈或好友会话
     */
    public static void sharePictureToWx(Context context, int drawable, int toShare) {
        if (MyApplication.getInstance().wxapi.isWXAppInstalled()) {
            Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), drawable);

            WXImageObject wxImageObject = new WXImageObject(bmp);
            WXMediaMessage msg = new WXMediaMessage();
            msg.mediaObject = wxImageObject;

            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmp, 100, 100, true);
            scaledBitmap.recycle();
            msg.thumbData = Utils.Bitmap2Bytes(scaledBitmap);

            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = "";//唯一标识
            req.message = msg;
            req.scene = toShare;
            if (MyApplication.getInstance().wxapi != null) {
                MyApplication.getInstance().wxapi.sendReq(req);
            }
        } else {
            ToastUtils.getInstance().toast("请先安装微信");
        }
    }

    /**
     * 分享网页到QQ
     *
     * @param url         分享网址
     * @param title       分享标题
     * @param drawableUrl 分享缩略图
     * @param description 分享描述信息
     */
    public static void sharePageToQQ(String url, String title, String drawableUrl, String description) {
        /*Platform.ShareParams shareParams = new Platform.ShareParams();
        shareParams.setTitle(title);
        shareParams.setText(description);
        shareParams.setImageUrl(drawableUrl);
        shareParams.setTitleUrl(url);

        Platform platform = ShareSDK.getPlatform(QQ.NAME);
        platform.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                ToastUtils.getInstance().toast("成功");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                Log.e("", "onError: " + throwable.getMessage());
                ToastUtils.getInstance().toast("失败");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                ToastUtils.getInstance().toast("取消");
            }
        });
        platform.share(shareParams);*/
    }
}
